/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/**************************************************
*
* Queue Class
*
* File: queueTask.cpp
*
* This class contains code for testing a Queue class
* in vxWorks for 1 producer and 1 consumer
*
* $Author: dixonc $
* $Date: 2005/05/03 04:39:32 $
* $Revision: 1.1.1.1 $
*
**************************************************/

#include "../Queue.h"
#include <iostream.h>
#include <stdlib.h>
#include <time.h>

#include "vxWorks.h"

// global queue
Queue que(26);

void producer();
void consumer();

void queueTest()
{
	int pid, cid;

	srand(time(NULL));
	pid = spawn( producer , ... );
	cid = spawn( consumer , ... );

	sleep(60);	

	taskDelete(pid);
	taskDelete(cid);
	
}

void producer()
{
	char c = 'A';
	struct timespec ts = {0,0};

	while()
	{
		if( que.Free() )
		{
			que.Push( &c );
			c++; 
			if( c == 'Z' + 1)
				c = 'A';
		}
		ts.tv_nsec = 1 + (int)( 1000000000.0 * rand() / (RAND_MAX+1.0));
		nanosleep( &ts );
	
	}
}
	
	
void consumer()
{
	char c='\0', last = 'A' - 1;
	struct timespec ts = {0,0};
	
	while(){

		if( que.Used() ) {
			que.Pop( &c );
		
			if( c != last + 1 )
				cout <<"****** ERROR last = " << last << " c = " <<c << endl;

			last = c;
		} 
			
		ts.tv_nsec = 1 + (int)( 1000000000.0 * rand() / (RAND_MAX+1.0));
		nanosleep( &ts );
	}

}
